package cn.stronglink.assetmanage.utils.dao;

import java.util.ArrayList;
import java.util.List;

import cn.stronglink.assetmanage.MyApplication;
import cn.stronglink.assetmanage.dao.CheckDao;
import cn.stronglink.assetmanage.dao.DaoManager;
import cn.stronglink.assetmanage.dao.DaoSession;
import cn.stronglink.assetmanage.dao.InventoryDao;
import cn.stronglink.assetmanage.dao.PatrolDao;
import cn.stronglink.assetmanage.dao.RepairDao;
import cn.stronglink.assetmanage.entity.Check;
import cn.stronglink.assetmanage.entity.Inventory;
import cn.stronglink.assetmanage.entity.Patrol;
import cn.stronglink.assetmanage.entity.Repair;
import cn.stronglink.assetmanage.entity.Task;

/**
 * Author by chencongcong,Date on 2018/9/24
 */
public class TaskDataUtil {
    public static List<Task> getFinishTaskNoDistance() {
        List<Task> taskList = new ArrayList<>();
        DaoSession session = DaoManager.getDaoSession(MyApplication.APP_CONTEXT);
        List<Check> checkList = session.getCheckDao().queryBuilder().where(CheckDao.Properties.Status.eq(4)).list();
        List<Inventory> inventorieList = session.getInventoryDao().queryBuilder().where(InventoryDao.Properties.Status.eq(1)).list();
        List<Repair> repairList = session.getRepairDao().queryBuilder().where(RepairDao.Properties.Status.eq(3)).list();
        List<Patrol> patrolList = session.getPatrolDao().queryBuilder().where(PatrolDao.Properties.Status.eq(3)).list();
        if (checkList != null && checkList.size() > 0) {
            for (Check check : checkList) {
                Task task = new Task();
                task.setId_(check.getId_());
                task.setStatus(check.getStatus());
                task.setBusinessType(1);
                task.setStatusName("已入库");
                task.setCreateTime(check.getCreateTime());
                task.setCreateUser(check.getCreateUser());
                //  设置执行时间
                task.setExecuteTime(check.getExecutionTime());
                taskList.add(task);
            }
        }

        if (inventorieList != null && inventorieList.size() > 0) {
            for (Inventory inventory : inventorieList) {
                Task task = new Task();
                task.setId_(inventory.getId_());
                task.setStatus(inventory.getStatus());
                task.setBusinessType(2);
                task.setStatusName("已盘点");
                // 创建时间
                task.setCreateTime(inventory.getCreateTime());
                // 创建人
                task.setCreateUser(inventory.getCreateName());
                task.setExecuteTime(inventory.getExecuteTime());
                taskList.add(task);
            }
        }

        if (repairList != null && repairList.size() > 0) {
            for (Repair repair : repairList) {
                Task task = new Task();
                task.setId_(repair.getId_());
                task.setStatus(repair.getStatus());
                task.setBusinessType(3);
                task.setStatusName("已维修");
                task.setCreateTime(repair.getCreateTime());
                task.setCreateUser(repair.getCreateName());
                task.setExecuteTime(repair.getExecuteTime());
                taskList.add(task);
            }
        }

        if (patrolList != null && patrolList.size() > 0) {
            for (Patrol patrol : patrolList) {
                Task task = new Task();
                task.setId_(patrol.getId_());
                task.setStatus(patrol.getStatus());
                task.setBusinessType(4);
                task.setStatusName("已巡检");
                // 创建时间
                task.setCreateTime(patrol.getCreateName());
                task.setCreateUser(patrol.getOperatiorName());
                // 执行时间
                task.setExecuteTime(patrol.getBeginTime());
                taskList.add(task);
            }
        }
        return taskList;
    }

    public static List<Task> getUnfinishTaskNoDisntance() {
        List<Task> taskList = new ArrayList<>();
        DaoSession session = DaoManager.getDaoSession(MyApplication.APP_CONTEXT);
        List<Check> checkList = session.getCheckDao().queryBuilder().where(CheckDao.Properties.Status.notEq(4)).list();
        List<Inventory> inventorieList = session.getInventoryDao().queryBuilder().where(InventoryDao.Properties.Status.notEq(1)).list();
        List<Repair> repairList = session.getRepairDao().queryBuilder().where(RepairDao.Properties.Status.notEq(3)).list();
        List<Patrol> patrolList = session.getPatrolDao().queryBuilder().where(PatrolDao.Properties.Status.notEq(3)).list();
        if (checkList != null && checkList.size() > 0) {
            for (Check check : checkList) {
                Task task = new Task();
                task.setId_(check.getId_());
                task.setStatus(check.getStatus());
                task.setBusinessType(1);
                task.setStatusName("待入库");
                task.setCreateTime(check.getCreateTime());
                task.setCreateUser(check.getCreateUser());
                //  设置执行时间
                task.setExecuteTime(check.getExecutionTime());
                taskList.add(task);
            }
        }

        if (inventorieList != null && inventorieList.size() > 0) {
            for (Inventory inventory : inventorieList) {
                Task task = new Task();
                task.setId_(inventory.getId_());
                task.setStatus(inventory.getStatus());
                task.setBusinessType(2);
                task.setStatusName("待盘点");
                // 创建时间
                task.setCreateTime(inventory.getCreateTime());
                // 创建人
                task.setCreateUser(inventory.getCreateName());
                task.setExecuteTime(inventory.getExecuteTime());
                taskList.add(task);
            }
        }

        if (repairList != null && repairList.size() > 0) {
            for (Repair repair : repairList) {
                Task task = new Task();
                task.setId_(repair.getId_());
                task.setStatus(repair.getStatus());
                task.setBusinessType(3);
                task.setStatusName("待维修");
                task.setCreateTime(repair.getCreateTime());
                task.setCreateUser(repair.getCreateName());
                task.setExecuteTime(repair.getExecuteTime());
                taskList.add(task);
            }
        }

        if (patrolList != null && patrolList.size() > 0) {
            for (Patrol patrol : patrolList) {
                Task task = new Task();
                task.setId_(patrol.getId_());
                task.setStatus(patrol.getStatus());
                task.setBusinessType(4);
                task.setStatusName("待巡检");
                // 创建时间
                task.setCreateTime(patrol.getCreateName());
                //创建人
                task.setCreateUser(patrol.getOperatiorName());
                //执行时间
                task.setExecuteTime(patrol.getBeginTime());
                taskList.add(task);
            }
        }

        return taskList;
    }
}
